package com.atguigu.gulimall.order.interceptor;

import com.atguigu.common.vo.MemberEntity;
import com.atguigu.gulimall.order.constant.OrderConstant;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
public class OrderInterceptor implements HandlerInterceptor {
    public static final ThreadLocal<MemberEntity> threadLocal = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getRequestURI().matches("/order/order/getOrderEntityByOrderSn")){
            return true;
        }
        if(request.getRequestURI().matches("/payed/notify")){
            return true;
        }
        MemberEntity attribute = (MemberEntity) request.getSession().getAttribute(OrderConstant.SPRING_SESSION_REDIS_KEY);
        if(attribute!=null){
            threadLocal.set(attribute);
            return true;
        }else {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.println("<script>alert('请先进行登录，再进行后续操作！');location.href='http://auth.gulimall.com/login.html'</script>");
            return false;
        }

    }
}
